<?php

/**
 * Controller_link
 */


class Controller_link extends MyApp {



	/** 首页 **/
	public function actionIndex ()
	{
		$link_lists = $this->db->fetchAll("SELECT * FROM `tbl_link` ORDER BY link_order ASC");

		$this->tpl->assign("link_lists", $link_lists);
		$this->tpl->display("link/link.index.tpl.php");
	}

	/** 链接列表 **/
	public function actionList ()
	{

		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_link`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$link_lists = $this->db->fetchAll("SELECT * FROM `tbl_link` ORDER BY link_id DESC LIMIT {$start}, ".PAGE_LIMIT."");



		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("nav_active", "admin");
		$this->tpl->assign("menu_active", "ad_list");

		$this->tpl->assign("link_lists", $link_lists);
		$this->tpl->display("link/link.list.tpl.php");
	}

	/** 顺序排序 **/
	public function actionOrder ()
	{
		$link_lists = $this->db->fetchAll("SELECT link_id FROM `tbl_link`");
		foreach ($link_lists as $key => $value){
			$link_id = $value['link_id'];
			if (!empty($_POST["link_order_{$link_id}"])){

				$info = array(
				"link_order" => $_POST["link_order_{$link_id}"],
				);

				$this->db->do_update("tbl_link", $info, "link_id='{$link_id}'");

			}

		}


		// 跳转
		Helper_Client::location("排序成功", "index.php?a=link.list");
	}

	/** 添加链接 **/
	public function actionAdd ()
	{
		if ($_POST){

			$link_name = $this->post->link_name;
			$link_url  = $this->post->link_url;

			$info = array(
			"link_name"   => $link_name,
			"link_url"    => $link_url,
			"status"      => 1,
			"create_time" => date("Y-m-d H:i:s"),
			"update_time" => "0000-00-00 00:00:00",
			);
			$this->db->do_insert("tbl_link", $info);


			// 跳转
			Helper_Client::location("添加成功", "index.php?a=link.list");


		}

		$this->tpl->display("link/link.add.tpl.php");
	}


	/** 修改链接 **/
	public function actionUpdate ()
	{
		$link_id = (int)$this->get->link_id;

		$link_data = $this->db->fetchRow("SELECT * FROM `tbl_link` WHERE link_id='{$link_id}'");

		if ($_POST){

			$link_name = $this->post->link_name;
			$link_url  = $this->post->link_url;

			$info = array(
			"link_name"     => $link_name,
			"link_url"      => $link_url,
			"update_time" => date("Y-m-d H:i:s"),

			);
			$this->db->do_update("tbl_link", $info, "link_id='{$link_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=link.list");

		}

		$this->tpl->assign("link_data", $link_data);
		$this->tpl->display("link/link.update.tpl.php");
	}


	/** 删除链接 **/
	public function actionDel ()
	{
		$link_id = (int)$this->get->link_id;

		$sql = "DELETE FROM `tbl_link` WHERE link_id='{$link_id}'";
		$this->db->query($sql);

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=link.list");
	}

}



?>